package cn.ll.po;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Contract entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.ll.po.Contract
 * @author MyEclipse Persistence Tools
 */

public class ContractDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(ContractDAO.class);
	// property constants
	public static final String CUSTOMERID = "customerid";
	public static final String CUSTOMERNAME = "customername";
	public static final String USERID = "userid";
	public static final String USER_NAME = "userName";
	public static final String STATUS = "status";
	public static final String VERIFYUSER = "verifyuser";
	public static final String PRONAME = "proname";
	public static final String REGION = "region";
	public static final String COUNTRY = "country";
	public static final String CITY = "city";
	public static final String SIZE = "size";
	public static final String CURRENCY = "currency";
	public static final String TYPE = "type";
	public static final String ROOTID = "rootid";
	public static final String COST = "cost";
	public static final String COST_RMB = "costRmb";
	public static final String REQUIREMENTS = "requirements";
	public static final String CONTACTID = "contactid";

	protected void initDao() {
		// do nothing
	}

	public void save(Contract transientInstance) {
		log.debug("saving Contract instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Contract persistentInstance) {
		log.debug("deleting Contract instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Contract findById(String id) {
		log.debug("getting Contract instance with id: " + id);
		try {
			Contract instance = (Contract) getHibernateTemplate().get("cn.ll.po.Contract", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Contract> findByExample(Contract instance) {
		log.debug("finding Contract instance by example");
		try {
			List<Contract> results = (List<Contract>) getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Contract instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Contract as model where model." + propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Contract> findByCustomerid(Object customerid) {
		return findByProperty(CUSTOMERID, customerid);
	}

	public List<Contract> findByCustomername(Object customername) {
		return findByProperty(CUSTOMERNAME, customername);
	}

	public List<Contract> findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}

	public List<Contract> findByUserName(Object userName) {
		return findByProperty(USER_NAME, userName);
	}

	public List<Contract> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List<Contract> findByVerifyuser(Object verifyuser) {
		return findByProperty(VERIFYUSER, verifyuser);
	}

	public List<Contract> findByProname(Object proname) {
		return findByProperty(PRONAME, proname);
	}

	public List<Contract> findByRegion(Object region) {
		return findByProperty(REGION, region);
	}

	public List<Contract> findByCountry(Object country) {
		return findByProperty(COUNTRY, country);
	}

	public List<Contract> findByCity(Object city) {
		return findByProperty(CITY, city);
	}

	public List<Contract> findBySize(Object size) {
		return findByProperty(SIZE, size);
	}

	public List<Contract> findByCurrency(Object currency) {
		return findByProperty(CURRENCY, currency);
	}

	public List<Contract> findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List<Contract> findByRootid(Object rootid) {
		return findByProperty(ROOTID, rootid);
	}

	public List<Contract> findByCost(Object cost) {
		return findByProperty(COST, cost);
	}

	public List<Contract> findByCostRmb(Object costRmb) {
		return findByProperty(COST_RMB, costRmb);
	}

	public List<Contract> findByRequirements(Object requirements) {
		return findByProperty(REQUIREMENTS, requirements);
	}

	public List<Contract> findByContactid(Object contactid) {
		return findByProperty(CONTACTID, contactid);
	}

	public List findAll() {
		log.debug("finding all Contract instances");
		try {
			String queryString = "from Contract";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/**
	 * //根据销售人员的ID,获取协议列表//
	 * @param uid
	 * @param start
	 * @param limit
	 * @return
	 */

	public Map selectSelfContractList(String uid, int start, int limit) {
		log.debug("finding self Contract instances");
		try {
			String queryString = "from Contract where userid ='" + uid + "' order by createtime desc";
			List list = getHibernateTemplate().find(queryString);
			int size = list.size();
			Map map = new HashMap(0);
			int toIndex = start + limit > size ? size : start + limit;
			map.put("list", list.subList(start, toIndex));
			map.put("totalcount", size);
			return map;
		} catch (RuntimeException re) {
			log.error("find self contract failed", re);
			throw re;
		}
	}

	/**
	 * 根据协议的状态返回列表
	 * @param status
	 * @param start
	 * @param limit
	 * @return
	 */
	public Map findByStatus(int status, int start, int limit) {

		List list = findByProperty(STATUS, status);
		int size = list.size();
		Map map = new HashMap(0);
		int toIndex = start + limit > size ? size : start + limit;
		map.put("list", list.subList(start, toIndex));
		map.put("totalcount", size);
		return map;
	}

	/**
	 * 获取所有的协议列表
	 * @param start
	 * @param limit
	 * @return
	 */
	public Map selectAllContractList(int start, int limit) {
		log.debug("finding all Contract instances");
		try {
			String queryString = "from Contract order by createtime desc";
			List list = getHibernateTemplate().find(queryString);
			int size = list.size();
			Map map = new HashMap(0);
			int toIndex = start + limit > size ? size : start + limit;
			map.put("list", list.subList(start, toIndex));
			map.put("totalcount", size);
			return map;
		} catch (RuntimeException re) {
			log.error("find all contract failed", re);
			throw re;
		}
	}

	public Contract merge(Contract detachedInstance) {
		log.debug("merging Contract instance");
		try {
			Contract result = (Contract) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Contract instance) {
		log.debug("attaching dirty Contract instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Contract instance) {
		log.debug("attaching clean Contract instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ContractDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ContractDAO) ctx.getBean("ContractDAO");
	}

	public long countByIdPatten(String idPatten) {

		log.debug("count instances by hql");
		try {
			String queryString = "select count(*) from Contract as model where model.id like '" + idPatten + "'";

			List list = this.getHibernateTemplate().find(queryString);

			Long count = (Long) list.get(0);

			return count;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}

	}
}